package com.gulimall.auth.controller;

import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.common.utils.R;
import com.atguigu.common.vo.MemberResp;
import com.gulimall.auth.service.OAuth2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * @Description
 * @Author lhj
 * @Date 2024/3/7 22:57
 */

@Controller
@RequestMapping("/oauth2/weibo")
public class OAuth2Controller {

    @Autowired
    OAuth2Service oAuth2Service;

    @GetMapping("/login/callbcak")
    public String loginCallback(@RequestParam(value = "code",required = false) String code , HttpSession session) {

        R result = oAuth2Service.loginCallback(code);
        if (result.getCode() != 0) {
            return "redirect:http://auth.gulimall.com/register.html";
        }
        MemberResp memberResp = result.getData(new TypeReference<MemberResp>() {});
        session.setAttribute(AuthServerConstant.LOGIN_USER, memberResp);

        return "redirect:http://gulimall.com";
    }

}
